﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ReconUI
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            chngPassword.ChangingPassword += new LoginCancelEventHandler(this.chngPassword_ChangedPassword);
        }

        protected void chngPassword_ChangedPassword(object sender, LoginCancelEventArgs e)
        {
            if (chngPassword.CurrentPassword.ToString() == chngPassword.NewPassword.ToString())
            {
                lblErrorMsg.Visible = true;
                lblErrorMsg.Text = "* Old password and new password must be different.  Please try again.";
                lblErrorMsg.ForeColor = System.Drawing.Color.Red;
                e.Cancel = true;
                HomeLink.Visible = false;
            }
            else if (chngPassword.CurrentPassword.ToString() == chngPassword.NewPassword.ToString())
            {
                lblErrorMsg.Visible = true;
                lblErrorMsg.Text = "* Old password and new password must be different.  Please try again.";
                lblErrorMsg.ForeColor = System.Drawing.Color.Red;
                e.Cancel = true;
                HomeLink.Visible = false;
            }
            else
            {
                UserVO loggedUser = (UserVO)HttpContext.Current.Session["CurrentUserVO"];
                DBFactory db = new DBFactory();
                int retValue = db.changePassword(chngPassword.NewPassword, chngPassword.CurrentPassword, loggedUser);
                if (retValue == 1)
                {
                    e.Cancel = true;
                    HomeLink.Text = "Go To Home Page";
                    HomeLink.Visible = true;
                    HomeLink.NavigateUrl = "ReconHome.aspx";
                    lblErrorMsg.Text = "* Password changed successfully.";
                    lblErrorMsg.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    lblErrorMsg.Visible = true;
                    lblErrorMsg.Text = "* Invalid user. Please try again.";
                    lblErrorMsg.ForeColor = System.Drawing.Color.Red;
                    e.Cancel = true;
                    HomeLink.Visible = false;
                }

            }

        }
    }
}